package com.zhejiangzhengyuan.municipal_common.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * @author lin_ju
 * @create on 2019/11/26
 */
public class DateFormatUtil {
  public static Timestamp timeFormat(long timestamp) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return Timestamp.valueOf(simpleDateFormat.format(calendarFormat(timestamp).getTime()));
  }

  public static Calendar calendarFormat(long timestamp) {
    Calendar date = Calendar.getInstance();
    date.setTimeInMillis(timestamp);
    return date;
  }

  /**
   * 根据时间格式，和对应时间返回需要的时间戳
   *
   * @param datePattern 时间格式
   * @param time 具体时间
   * @return 时间戳
   * @throws ParseException 若时间格式与提交时间不一致会抛出当前错误
   */
  public static long getTimeByString(String datePattern, String time) throws ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern);
    Date date = simpleDateFormat.parse(time);
    return date.getTime();
  }

  /**
   * 根据时间格式，和时间戳饭回需要的时间String
   *
   * @param datePattern 时间格式
   * @param timestamp 时间戳
   * @return 时间String
   */
  public static String getStringByTime(String datePattern, long timestamp) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern);
    return simpleDateFormat.format(calendarFormat(timestamp).getTime());
  }

  /**
   * 根据timestamp和周期获取开始时间
   *
   * @param period yearly/monthly/daily
   * @return 开始的时间戳
   */
  public static long getStartTimeByPeriod(String period, Long timestamp) {
    Calendar calendar = getDefaultCalenderInstance(timestamp);
    switch (period) {
      case "daily":
        break;
      case "monthly":
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        break;
      case "yearly":
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.MONTH, 0);
        break;
    }
    return calendar.getTimeInMillis();
  }

  /**
   * 根据timestamp和周期获取结束时间
   *
   * @param period yearly/monthly/daily
   * @return 开始的时间戳
   */
  public static long getEndTimeByPeriod(String period, Long timestamp) {

    Calendar calendar = getDefaultCalenderInstance(timestamp);
    switch (period) {
      case "daily":
        calendar.add(Calendar.DATE, 1);
        break;
      case "monthly":
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);
        break;
      case "yearly":
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.MONTH, 0);
        calendar.add(Calendar.YEAR, 1);
        break;
    }
    return calendar.getTimeInMillis() - 1;
  }

  private static Calendar getDefaultCalenderInstance(Long timestamp) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timestamp);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar;
  }

  public static String getChartData(Integer month) {
    month = Objects.nonNull(month) ? month : 1;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
    String year = DateFormatUtil.getStringByTime("yyyy", System.currentTimeMillis());
    // 根据找到月份
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(Calendar.MONTH, month - 1);
    calendar.set(Calendar.YEAR, Integer.parseInt(year));
    return simpleDateFormat.format(calendar.getTimeInMillis());
  }
  // 根据年月季获取开始月份
  public static int getStartCalendarField(String granularity) {
    Calendar calendar = Calendar.getInstance();
    switch (granularity) {
      case "年":
        return 1;
      case "月":
        return calendar.get(Calendar.MONTH) + 1;
      case "季":
        return (calendar.get(Calendar.MONTH) / 3 * 3) + 1;
    }
    return 1;
  }
  // 根据年月季获取结束月份
  public static int getEndCalendarField(String granularity) {
    Calendar calendar = Calendar.getInstance();
    switch (granularity) {
      case "年":
        return 12;
      case "月":
        return calendar.get(Calendar.MONTH) + 1;
      case "季":
        return (calendar.get(Calendar.MONTH) / 3 * 3) + 4;
    }
    return 12;
  }
}
